import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;

public class ReadFile {
	private static BufferedWriter bwNode, bwEdge;
	private static int counter = 1, sourceCounter = 0, destinationCounter = 0;
	static String source, destination;
	static boolean startOfEdge = true;
	static int start = 0;

	public static void fileInput() {

		try {
			bwNode = new BufferedWriter(new FileWriter("nodes.txt"));
			bwEdge = new BufferedWriter(new FileWriter("edges.txt"));
			ReadFileXML("map.osm");
			bwNode.close();
			bwEdge.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void ReadFileXML(String str) {
		try {
			SAXParserFactory parserFact = SAXParserFactory.newInstance();
			SAXParser parser = parserFact.newSAXParser();
			DefaultHandler dHandler = new DefaultHandler() {

				public void startElement(String uri, String localName,
						String element_name, Attributes attributes)
						throws SAXException {

					try {
						if (element_name.equals("node")

						// && (sourceCounter + destinationCounter + counter) <=
						// ShortestPath.n
						) {
							if (source.equals(attributes.getValue("id"))) {
								bwNode.write(attributes.getValue("id") + "\n");
								bwNode.write(String.valueOf(0) + "\n");
								bwNode.write(attributes.getValue("lat") + "\n");
								bwNode.write(attributes.getValue("lon") + "\n");
								bwNode.write("\n");
								sourceCounter++;
							} else if (destination.equals(attributes
									.getValue("id"))) {
								bwNode.write(attributes.getValue("id") + "\n");
								bwNode.write(String.valueOf(ShortestPath.n - 1)
										+ "\n");
								bwNode.write(attributes.getValue("lat") + "\n");
								bwNode.write(attributes.getValue("lon") + "\n");
								bwNode.write("\n");
								destinationCounter++;
							} else {
								bwNode.write(attributes.getValue("id") + "\n");
								bwNode.write(String.valueOf(counter++) + "\n");
								bwNode.write(attributes.getValue("lat") + "\n");
								bwNode.write(attributes.getValue("lon") + "\n");
								bwNode.write("\n");
							}

						}
						if (element_name.equals("nd")) {
							if (startOfEdge) {
								start++;
								bwEdge.write(attributes.getValue("ref") + ",");
							} else {
								startOfEdge = true;
								bwEdge.write("\n");
								bwEdge.write(attributes.getValue("ref") + ",");
							}
						} else {
							if (start != 0)
								startOfEdge = false;
						}
					} catch (Exception e) {
						System.out.println("error in reading from file");
					}

				}
			};

			parser.parse(str, dHandler);

		} catch (Exception e) {
			System.out.println("XML File hasn't any elements");
			e.printStackTrace();
		}
	}

	public static void readEdgesFile(String fileName) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(fileName));
			String line;
			String[] edgeList;

			while ((line = br.readLine()) != null) {
				edgeList = line.split(",");
				for (int i = 0; i < edgeList.length - 1; i++) {
					try {
						double lat1 = ShortestPath.hashNode.get(
								(ShortestPath.hashMapOfMappingNumbers
										.get(edgeList[i].toString())))
								.getLatitude();
						double lng1 = ShortestPath.hashNode.get(
								(ShortestPath.hashMapOfMappingNumbers
										.get(edgeList[i].toString())))
								.getLongitude();
						double lat2 = ShortestPath.hashNode.get(
								(ShortestPath.hashMapOfMappingNumbers
										.get(edgeList[i + 1].toString())))
								.getLatitude();
						double lng2 = ShortestPath.hashNode.get(
								(ShortestPath.hashMapOfMappingNumbers
										.get(edgeList[i + 1].toString())))
								.getLongitude();
						int index1, index2;
						index1 = ShortestPath.hashMapOfMappingNumbers
								.get(edgeList[i].toString());
						index2 = ShortestPath.hashMapOfMappingNumbers
								.get(edgeList[i + 1].toString());

						ShortestPath.linkcost[index1][index2] = ShortestPath
								.haversineDistance(lat1, lng1, lat2, lng2);
					} catch (Exception e) {
						// e.printStackTrace();
					}
				}
			}
		} catch (Exception e) {

		}
	}

	public static String getSource() {
		return source;
	}

	public static void setSource(String source) {
		ReadFile.source = source;
	}

	public static String getDestination() {
		return destination;
	}

	public static void setDestination(String destination) {
		ReadFile.destination = destination;
	}
}